package com.ddwl.gate.config;

import com.ddwl.common.constant.RabbitMqConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author hemiaojun
 * @Date 2020/9/27 10:45
 */
@Configuration
public class MobileRechargeMqConfig {

    /**
     *
     *  手机充值订单超时交换器
     * @return the exchange
     */
    @Bean("mobileRechargeExchange")
    public Exchange mobileRechargeExchange() {
        return ExchangeBuilder.directExchange(RabbitMqConstants.MOBILE_RECHARGE_ORDER_TIMEOUT_DL_EXCHANGE).durable(true).build();
    }

    /**
     * 手机充值订单转发队列
     *
     * @return the queue
     */
    @Bean("mobileRechargeRedirectQueue")
    public Queue mobileRechargeRedirectQueue() {
        return QueueBuilder.durable(RabbitMqConstants.MOBILE_RECHARGE_ORDER_TIMEOUT_REDIRECT_QUEUE).build();
    }

    /**
     * 转发队列绑定交换器
     *
     * @return the binding
     */
    @Bean
    public Binding mobileRechargeRedirectBinding() {
        return new Binding(RabbitMqConstants.MOBILE_RECHARGE_ORDER_TIMEOUT_REDIRECT_QUEUE, Binding.DestinationType.QUEUE, RabbitMqConstants.MOBILE_RECHARGE_ORDER_TIMEOUT_DL_EXCHANGE, RabbitMqConstants.MOBILE_RECHARGE_ORDER_TIMEOUT_REDIRECT_QUEUE, null);
    }

    /**
     * 死信队列绑定交换器
     *
     * @return the binding
     */
    @Bean
    public Binding mobileRechargeDeadLetterBinding() {
        return new Binding(RabbitMqConstants.MOBILE_RECHARGE_ORDER_TIMEOUT_DL_QUEUE, Binding.DestinationType.QUEUE, RabbitMqConstants.MOBILE_RECHARGE_ORDER_TIMEOUT_DL_EXCHANGE, RabbitMqConstants.MOBILE_RECHARGE_ORDER_TIMEOUT_DL_QUEUE, null);
    }

    /**
     * 手机充值订单死信队列.
     *
     * @return the queue
     */
    @Bean("mobileRechargeDeadLetterQueue")
    public Queue mobileRechargeDeadLetterQueue() {
        Map<String, Object> args = new HashMap<>(3);
        //设置转交换器
        args.put("x-dead-letter-exchange", RabbitMqConstants.MOBILE_RECHARGE_ORDER_TIMEOUT_DL_EXCHANGE);
        //设置转发交换器key
        args.put("x-dead-letter-routing-key", RabbitMqConstants.MOBILE_RECHARGE_ORDER_TIMEOUT_REDIRECT_QUEUE);
        //设置过期时间 单位毫秒
        args.put("x-message-ttl", 15*60*1000);
        return QueueBuilder.durable(RabbitMqConstants.MOBILE_RECHARGE_ORDER_TIMEOUT_DL_QUEUE).withArguments(args).build();
    }


}
